//  Copyright (c) 2008  Yoong-ki Ahn & Youngjin Kim < mexif@googlegroups.com>
//  See the enclosed file COPYING for license information (MIT Licence).
//  If you did not receive this file,
//  see http://www.opensource.org/licenses/mit-license.php.

package mexif;

public class ByteUtil {
	
	static public int get16u(byte[] data, int offset, boolean littleEndian) {
		int a = data[offset] & 0xFF;
		int b = data[offset+1] & 0xFF;
		
		if( littleEndian )
			return (( b << 8 ) | a);
		else
			return (( a << 8 ) | b);
	}
	
	static public long get32u(byte[] data, int offset, boolean littleEndian) {
		int a = data[offset] & 0xFF;
		int b = data[offset+1] & 0xFF;
		int c = data[offset+2] & 0xFF;
		int d = data[offset+3] & 0xFF;
		
		if( littleEndian )
			return (( d<<24 ) | (c<<16) | (b<<8) | a);
		else
			return (( a<<24 ) | (b<<16) | (c<<8) | d);
	}
	
    public byte[] getBytes(byte[] data, int offset, int length) {
    	if (data == null || length == 0)
    		return null;

		byte[] raw = new byte[length];
		for (int count = offset; length > 0; count++, length--) {
		    raw[count-offset] = data[count];
		}

		return raw;
	}
}
